<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>树形结构渲染</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
            padding: 20px;
            color: rgba(0, 0, 0, 0.85);
        }
        
       
        
    </style>
</head>
<body>
    <div class="container">
        <h2>树形结构渲染示例</h2>
        <div id="zselect"></div>
        1122333
    </div>
    <script src="select2.js"></script>
    <script>
        // 树形数据
        const treeData = [
            {
                id: 1,
                title: '分类 1',
                children: [
                    {
                        id: 11,
                        title: '分类 1-1'
                    },
                    {
                        id: 12,
                        title: '分类 1-2',
                        children: [
                            {
                                id: 121,
                                title: '分类 1-2-1'
                            },
                            {
                                id: 122,
                                title: '分类 1-2-2'
                            },
                            {
                                id: 123,
                                title: '分类 1-2-3'
                            }
                        ]
                    },
                    {
                        id: 13,
                        title: '分类 1-3',
                        children: [
                            {
                                id: 131,
                                title: '分类 1-3-1'
                            },
                            {
                                id: 132,
                                title: '分类 1-3-2'
                            },
                            {
                                id: 133,
                                title: '分类 1-3-3',
                                children: [
                                    {
                                        id: 1331,
                                        title: '分类 1-3-3-1'
                                    },
                                    {
                                        id: 1332,
                                        title: '分类 1-3-3-2'
                                    },
                                    {
                                        id: 1333,
                                        title: '分类 1-3-3-3'
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                id: 2,
                title: '分类 2',
                children: [
                    {
                        id: 21,
                        title: '分类 2-1'
                    },
                    {
                        id: 22,
                        title: '分类 2-2'
                    }
                ]
            },
            {
                id: 3,
                title: '分类 3'
            },
            {
                id: 4,
                title: '分类 4',
                children: [
                    {
                        id: 41,
                        title: '分类 4-1'
                    }
                ]
            },
            {
                id: 5,
                title: '分类 5',
                children: [
                    {
                        id: 51,
                        title: '分类 5-1'
                    }
                ]
            },{
                id: 6,
                title: '分类 6',
                children: [
                    {
                        id: 61,
                        title: '分类 6-1'
                    }
                ]
            },{
                id: 7,
                title: '分类 7',
                children: [
                    {
                        id: 71,
                        title: '分类 7-1'
                    }
                ]
            },{
                id: 8,
                title: '分类 8',
                children: [
                    {
                        id: 81,
                        title: '分类 8-1'
                    }
                ]
            }
        ];
        
        const zselect = document.getElementById('zselect')
        
        new Select2(zselect, {
            treeData:treeData,
            placeholder: '请选择',
            selectVal: [11, 121,122,123, 81, 21, 22, 131, 132, 1331, 1332, 1333],
            onSelected: function(node){
                const arr = []
                node.closest('.tree').querySelectorAll('.leaf').forEach(res=>{
                    res.querySelector('.checkbox-completed')&&  arr.push(res.querySelector('.checkbox-completed')?.data)
                })
            }
        })
    </script>
</body>
</html>